<template>
  <div class="app-container">
    <div class="search-area" v-show="showSearch">
      <el-form
        :model="queryParams"
        @submit.native.prevent
        ref="queryForm"
        size="small"
        :inline="true"
        class="clearfix"
        label-width="auto"
      >
        <el-form-item label="拓展名称" prop="expansionName">
          <el-input
            v-model="queryParams.expansionName"
            placeholder="请输入拓展名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="拓展类型" prop="expansionTypeList">
          <!-- <el-select
            v-model="queryParams.expansionTypeList"
            placeholder="请选择拓展类型"
            clearable
            @keyup.enter.native="handleQuery"
          >
            <el-option
              v-for="dict in dict.type.expansion_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select> -->
          <el-select multiple collapse-tags v-model="queryParams.expansionTypeList" placeholder="请选择拓展类型">
            <el-option-group v-for="group in typeList" :key="group.label" :label="group.label">
              <el-option v-for="item in group.children" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item label="拓展时间" prop="timeList">
          <el-date-picker
            v-model="queryParams.timeList"
            style="width: 220px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="main-content">
      <el-row :gutter="10" class="mb24">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['biz:market:expansion:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['biz:market:expansion:remove']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button plain size="mini" @click="downloadTemplate()" v-hasPermi="['biz:market:expansion:import']"
            >下载导入模板</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-plus"
            size="mini"
            @click="importData()"
            v-hasPermi="['biz:market:expansion:import']"
            >导入</el-button
          >
          <input
            type="file"
            @change="onFileChange"
            ref="uploadFileInput"
            enctype="multipart/form-data"
            style="display: none"
            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
          />
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="expansionList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55px" align="center" />
        <el-table-column label="拓展名称" align="center" prop="expansionName" v-if="columns.expansionName.visible"  width="280"  fixed />
        <el-table-column
          label="拓展时间"
          align="center"
          prop="expansionTime"
          v-if="columns.expansionTime.visible"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.expansionTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="地址id" align="center" prop="addressId" v-if="columns.addressId.visible" /> -->
        <el-table-column label="对应项目" align="center" prop="target" v-if="columns.projectName.visible"  width="180" >
          <template slot-scope="scope">
            {{ scope.row.projectName }}
          </template>
        </el-table-column>
        <el-table-column label="对应客户" align="center" prop="target" v-if="columns.customerName.visible"  width="180" >
          <template slot-scope="scope">
            {{ scope.row.customerName }}
          </template>
        </el-table-column>
        <el-table-column label="支出预算" align="center" prop="expensesBudget" v-if="columns.expensesBudget.visible">
          <template slot-scope="scope">
            {{ scope.row.expensesBudget ? scope.row.expensesBudget.toFixed(2) : '' }}
          </template>
        </el-table-column>
        <el-table-column label="支出" align="center" prop="expenses" v-if="columns.expenses.visible">
          <template slot-scope="scope">
            <span :style="{ color: scope.row.expenses > scope.row.expensesBudget ? 'red' : '' }">
              {{ scope.row.expenses ? scope.row.expenses.toFixed(2) : '' }}</span
            >
          </template>
        </el-table-column>
        <el-table-column
          label="负责人"
          align="center"
          prop="responsiblePerson"
          v-if="columns.responsiblePerson.visible"
        />
        <el-table-column label="参加人员" align="center" prop="participants" v-if="columns.participants.visible" />
        <el-table-column label="拓展类型" align="center" v-if="columns.expansionType.visible"  width="180" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.expansion_type_level" :value="scope.row.expansionType" />
          </template>
        </el-table-column>
        <!-- <el-table-column label="项目名称" align="center" prop="projectName" v-if="columns.projectName.visible" />
        <el-table-column label="客户名称" align="center" prop="customerName" v-if="columns.customerName.visible" /> -->
        <!-- </el-table-column label="成果" align="center" prop="results" v-if="columns.results.visible" /> -->
        <!-- <el-table-column label="经度" align="center" prop="longitude" v-if="columns.longitude.visible" />
        <el-table-column label="纬度" align="center" prop="latitude" v-if="columns.latitude.visible" />
        <el-table-column label="作者" align="center" prop="author" v-if="columns.author.visible" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width"   width="180"  fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-tickets"
              @click="handleUpdate(scope.row, true)"
              v-hasPermi="['biz:market:expansion:getInfo']"
              >查看</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['biz:market:expansion:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['biz:market:expansion:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <expand-details
        :show="showDetails"
        :disabled="disabled"
        :id="expansionId"
        @refresh="getList"
        @cancel="showDetails = false"
      ></expand-details>
    </div>
  </div>
</template>

<script>
import { listExpansion, getExpansion, delExpansion, getExpansionType, importMarketExpansion } from '@/api/biz/expansion'
import ExpandDetails from './ExpansionDetails.vue'
export default {
  name: 'ExpansionList',
  components: {
    ExpandDetails,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 市场拓展表格数据
      expansionList: [],
      // 弹出层标题
      title: '',
      // 是否禁止表单填写
      disabled: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        expansionName: null,
        expansionTypeList: null,
        timeList: [],
        projectName: null,
        customerName: null,
      },
      // 列信息
      columns: {
        expansionName: { label: '拓展名称', visible: true },
        expansionTime: { label: '拓展时间', visible: true },
        // addressId: { label: '地址id', visible: true },
        // target: { label: '对应项目/对应客户', visible: true },
        expensesBudget: { label: '支出预算', visible: true },
        expenses: { label: '支出', visible: true },
        responsiblePerson: { label: '负责人', visible: true },
        participants: { label: '参加人员', visible: true },
        expansionType: { label: '拓展类型', visible: true },
        projectName: { label: '项目名称', visible: true },
        customerName: { label: '客户名称', visible: true },
        // longitude: { label: '经度', visible: true },
        // latitude: { label: '纬度', visible: true },
        // author: { label: '作者', visible: true },
      },
      expansionId: null,
      showDetails: false,
      typeList: [],
    }
  },
  dicts: ['expansion_type_level'],
  created() {
    const { customerName, expansionType } = this.$route.query ? this.$route.query : {}
    this.queryParams.customerName = customerName
    this.queryParams.expansionType = expansionType
    this.getList()
    this.getTypes()
  },
  methods: {
    getTypes() {
      getExpansionType().then((response) => {
        this.typeList = response.data
      })
    },
    /** 查询市场拓展列表 */
    getList() {
      this.loading = true
      listExpansion(this.queryParams).then((response) => {
        this.expansionList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        addressId: null,
        author: null,
        expansionDetails: null,
        expansionName: null,
        expansionTime: null,
        expansionType: null,
        expenses: null,
        latitude: null,
        longitude: null,
        participants: null,
        responsiblePerson: null,
        results: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.customerName = null
      this.queryParams.expansionType = null
      this.queryParams = { ...this.queryParams }
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.disabled = false
      this.expansionId = null
      this.showDetails = true
    },
    /** 修改按钮操作 */
    handleUpdate(row, readOnly = false) {
      this.disabled = readOnly
      this.expansionId = row.id
      this.showDetails = true
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除市场拓展编号为"' + ids + '"的数据项？')
        .then(function () {
          return delExpansion(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'biz/expansion/export',
        {
          ...this.queryParams,
        },
        `expansion_${new Date().getTime()}.xlsx`
      )
    },
    onFileChange() {
      const formData = new FormData()
      formData.append('file', this.$refs.uploadFileInput.files[0])
      this.loading = true
      importMarketExpansion(formData)
        .then((response) => {
          this.loading = false
          this.$modal.msgSuccess('导入成功')
          // 清除文件内容，避免下次不生效
          this.$refs.uploadFileInput.value = ''
          this.resetQuery()
        })
        .catch((err) => {
          this.loading = false
          // 清除文件内容，避免下次不生效
          this.$refs.uploadFileInput.value = ''
        })
    },
    importData() {
      this.$refs.uploadFileInput.click()
    },
    downloadTemplate() {
      this.loading = true
      // 使用相对路径
      const fileUrl = '/expansionTemplate.xlsx'
      // 创建一个隐藏的a标签
      const link = document.createElement('a')
      link.href = fileUrl
      link.download = '市场拓展模板.xlsx' // 设置下载文件名
      // 将a标签添加到DOM并触发点击事件
      document.body.appendChild(link)
      link.click()
      // 移除a标签
      document.body.removeChild(link)
      this.loading = false
      this.$modal.msgSuccess('下载成功')
    },
  },
}
</script>
<style scoped lang="scss">
.el-date-editor,
.el-select {
  width: 100%;
}
</style>
